package com.epam.command.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.epam.command.logic.Command;
import com.epam.command.logic.LoginLogic;
import com.epam.util.ConfigurationManager;

public class LoginCommand implements Command {
	

	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) {
		
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		String page;
		
		if (LoginLogic.authorize(login, password)) {
			HttpSession session = request.getSession();
			LoginLogic.initializeSession(session, login);
			
			page =  ConfigurationManager.getConfig(ConfigurationManager.OPERATION_SUCCESSFUL);
					
		} else {
			System.out.println("here");
			request.setAttribute("errorMessage", "Invalid login or password");
			page = ConfigurationManager.getConfig(ConfigurationManager.LOGIN_PAGE);
			
		}

		return page;
	}
	
}